#!/bin/bash
# vim:ts=4:sw=4:expandtab

############################################################
# NAME
#     dgo - dox's tool for go into remote host
#
# SYNOPSIS
#     dgo { host-name | host-ip }
#
#     debug=on dgo ...
#
# AUTHORS
#     neiku project <ku7d@qq.com>
#
# SEE ALSO
#     doxrc_hosts
#
# VERSION
#     2016/03/19: 支持(无跳板机)快速登录指定主机(rsa/passwd)
#     2016/03/20: 支持(1层跳板机)快速登录指定主机(passwd)
#     2016/04/01: 支持(2层跳板机)快速登录指定主机(passwd)
#     2016/04/10: 支持基于dfw实现N层跳板机快速登录指定主机
#                 支持rsa/passwd登录验证方式
#     2016/05/10: 支持通过环境变量传递password，提高安全
#     2019/02/19: 默认使用home目录下的配置，提高灵活性
#                 eval赋值计算中，值部分补充单引号，增强可用性
#
#
############################################################

# help
function help()
{
    echo "Usage: dgo { host-name | host-ip }"
    echo "Report bugs to <ku7d@qq.com>"
}

# dgo { host-name | host-ip }
host="$1"
if [ "$host" = "" ] ; then
    echo "`help`" >&2
    exit 1
fi

# load host config
hosts=$HOME/doxrc_hosts
hname=; htype=; haddr=; hport=; huser=; hpass=; hproxys=;
eval `grep -v -E '^#|^$' $hosts 2>/dev/null                \
      | grep $host 2>/dev/null                             \
      | head -n1                                           \
      | awk '{printf "hname='\''%s'\''; htype='\''%s'\'';  \
                      haddr='\''%s'\''; hport='\''%s'\'';  \
                      huser='\''%s'\''; hpass='\''%s'\'';  \
                      hproxys='\''%s'\'';"                 \
                     , $1, $2                              \
                     , $3, $4                              \
                     , $5, $6                              \
                     , $7}'`

if [    -z "$hname" -o -z "$htype" \
     -o -z "$haddr" -o -z "$hport" \
     -o -z "$huser" -o -z "$hpass" ] ; then
    dlog error "check host config fail,"         \
               "hname:[$hname], htype:[$htype]," \
               "haddr:[$haddr], hport:[$hport]," \
               "huser:[$huser], hpass:[$hpass]"
    exit 1
fi
dlog debug "load host config succ,"          \
           "hname:[$hname], htype:[$htype]," \
           "haddr:[$haddr], hport:[$hport]," \
           "huser:[$huser], hpass:[$hpass]," \
           "hproxys:[$hproxys]"

# make tcp forwarding with dfw
tmp=/tmp/.doxtmp_$USER.$RANDOM
dfw --verbose $host > $tmp
if [ $? != 0 ] ; then
    dlog error "dfw fail, host:[$host]"
    rm -f $tmp
    exit 1
fi
llist="`cat $tmp`"
laddr="`echo "$llist" | tail -n1 | cut -d: -f1`"
lport="`echo "$llist" | tail -n1 | cut -d: -f2`"
dlog debug "dfw succ, host:[$host], tmpfile:[$tmp]," \
           ", llist:[$llist], laddr:[$laddr], lport:[$lport]"
rm -f $tmp

# login host
case "$htype" in
    rsa)
        ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no \
            -i $hpass -q -p $lport $huser@$laddr
        ;;
    passwd)
        DOX_DSSH_PASSWORD="$hpass" \
        dssh $huser "" $laddr $lport
        ;;
    *)
        dlog error "host type unsupported, type:[$htype]," \
                   "supported type:[rsa, passwd]"
        ;;
esac
